package com.joker.aigc.ai.controller;


import cn.hutool.core.collection.CollUtil;
import com.joker.aigc.ai.service.AiVoiceService;
import com.joker.aigc.domain.entity.R;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 上传格式文件
 *
 * @author : feixiang.li
 * @since : 2025-10-08 14:12
 */
@Slf4j
@RestController
@RequestMapping("/api/1.0/ai")
public class AiVoiceController {

    @Resource
    private AiVoiceService aiVoiceService;


    @RequestMapping("/upload")
    public R<List<String>> upload(@RequestParam("file") MultipartFile file) {
        return R.ok(aiVoiceService.upload(file));
    }

    @GetMapping("/getToken")
    public R<String> getToken() {
        return R.ok(aiVoiceService.getToken());
    }


    @GetMapping("/text")
    public R<List<String>> text(@RequestParam("text") String text) {
        return R.ok(aiVoiceService.text(text));
    }

    @Resource
    private ChatModel chatModel;

    @GetMapping("/chat")
    public R<String> chat(@RequestParam("text") String text) {
        List<ChatMessage> chatMessageList = new ArrayList<>();
        List<String> text1 = aiVoiceService.text(text);
        chatMessageList.add(SystemMessage.from("现在你是java求职者，正在面试，" +
                "请简单回答面试官问的问题，纯文字表达，300字以内"));

        if (CollUtil.isNotEmpty(text1)) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : text1) {
                stringBuilder.append(s);

            }
            stringBuilder.append("\n 以上知识库内容仅供参考。");
            chatMessageList.add(SystemMessage.from(stringBuilder.toString()));
        }
        chatMessageList.add(UserMessage.from("面试官问的问题:" + text));
        for (ChatMessage chatMessage : chatMessageList) {
            log.info("chatMessage:{}", chatMessage);
        }
        ChatResponse chat = chatModel.chat(chatMessageList);
        log.info("chat:{}", chat);
        return R.ok(chat.aiMessage().text());
    }

    @Resource
    private StreamingChatModel streamingChatModel;

    @GetMapping(value = "/streamText", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=UTF-8")
    public Flux<String> streamText(@RequestParam("text") String text) {
        List<ChatMessage> chatMessageList = new ArrayList<>();
        List<String> text1 = aiVoiceService.text(text);
        chatMessageList.add(SystemMessage.from("现在你是java求职者，正在面试，" +
                "请简单回答面试官问的问题，纯文字表达，300字以内"));

        if (CollUtil.isNotEmpty(text1)) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : text1) {
                stringBuilder.append(s);

            }
            stringBuilder.append("\n 以上知识库内容仅供参考。");
            chatMessageList.add(SystemMessage.from(stringBuilder.toString()));
        }
        chatMessageList.add(UserMessage.from("面试官问的问题:" + text));
        for (ChatMessage chatMessage : chatMessageList) {
            log.info("chatMessage:{}", chatMessage);
        }
        return Flux.create(emitter -> {
            aiVoiceService.streamText(text, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String partialResponse) {
                    emitter.next(partialResponse);
                }

                @Override
                public void onCompleteResponse(ChatResponse completeResponse) {
                    emitter.complete();
                }

                @Override
                public void onError(Throwable error) {
                    emitter.error(error);
                }
            });
        });
    }


    @RequestMapping("/uploadMarkdownFile")
    public R<String> uploadMarkdownFile(@RequestParam("file") MultipartFile file) {
        aiVoiceService.uploadMarkdownFile(file);
        return R.ok("上传成功");
    }
}
